function menutree(div,menuList) {
    var li = '<li> <a href=\"main.jsp\"><i class=\"fa fa-dashboard fa-fw\"></i> Dashboard</a> </li>';
    var dataJson = {
        "name": "一级菜单",
        "link": "text.jsp",
        "child": [
            {
                "link": "text.jsp",
                "name": "二级菜单1"
            },
            {
                "link": "text.jsp",
                "name": "二级菜单2",
                "child": [
                    {
                        "link": "text.jsp",
                        "name": "三级菜单1"
                    },
                    {
                        "link": "text.jsp",
                        "name": "三级菜单2"
                    },
                    {
                        "link": "text.jsp",
                        "name": "三级菜单3"
                    }
                ]
            }
        ]
    };
    var dataList = [
        {
            "ID":1,
            "PERMISSION_TYPE":"0",
            "PERMISSION_NAME":"发起流程",
            "URL":"text.jsp",
            "PARENT_ID":null,
            "IS_PARENT":1
        },
        {
            "ID":2,
            "PERMISSION_TYPE":"1",
            "PERMISSION_NAME":"我的流程",
            "URL":"text.jsp",
            "PARENT_ID":1,
            "IS_PARENT":0
        },
        {
            "ID":3,
            "PERMISSION_TYPE":"1",
            "PERMISSION_NAME":"待办流程",
            "URL":"text.jsp",
            "PARENT_ID":1,
            "IS_PARENT":0
        },
        {
            "ID":4,
            "PERMISSION_TYPE":"0",
            "PERMISSION_NAME":"人力资源",
            "URL":"text.jsp",
            "PARENT_ID":null,
            "IS_PARENT":1
        },
        {
            "ID":5,
            "PERMISSION_TYPE":"1",
            "PERMISSION_NAME":"人事档案",
            "URL":"text.jsp",
            "PARENT_ID":4,
            "IS_PARENT":0
        },
        {
            "ID":6,
            "PERMISSION_TYPE":"1",
            "PERMISSION_NAME":"考勤管理",
            "URL":"text.jsp",
            "PARENT_ID":4,
            "IS_PARENT":1
        },
        {
            "ID":7,
            "PERMISSION_TYPE":"2",
            "PERMISSION_NAME":"员工考勤",
            "URL":"text.jsp",
            "PARENT_ID":6,
            "IS_PARENT":0
        },
        {
            "ID":8,
            "PERMISSION_TYPE":"2",
            "PERMISSION_NAME":"考勤周期",
            "URL":"text.jsp",
            "PARENT_ID":6,
            "IS_PARENT":0
        }
    ];
    //addNode(div, buildFirstLevel(dataJson));
    addNode(div, buildFirstLevel2(dataList));

}



function addNode(div, node) {
    $(div).html($(div).html() + node);
}
function htmlEncode(str){
    return $('<div>').text(str).html();
}
function htmlDecode(html){
    return $('<div>').html(html).text();
}

function buildA(name,target,icon,arrow){
    if (icon == null){
        icon = "";
    }else{
        icon = "<i class=\"fa fa-files-o fa-fw\"></i>";
        name = " "+name;
    }
    if(arrow == null){
        arrow = "";
    }else{
        arrow = "<span class=\"fa arrow\"></span>";
    }
    if(target == null){
        target = "";
    }else{
        target = "onclick=\"openTab(\'"+target+ "\')\"";
    }
    var a = '<a href=\"javascript:void(0);\" '+target+'>'+icon + name +arrow+ '</a>';
    // alert(a);
    return a;
}

//方法一：以嵌套的json对象传入，最多3层菜单
//****************************start*****************************
function buildFirstLevel(data) {

    //var firstLevel = '<li> <a href=\"#\"><i class=\"fa fa-files-o fa-fw\"></i> ' + data.name + '<span class=\"fa arrow\"></span></a> <ul class=\"nav nav-second-level\"> ';
    var firstLevel = '<li>' + buildA( data.name, null, "1","1") + '<ul class=\"nav nav-second-level\"> ';
    for (var i = 0; i < data.child.length; i++) {
        firstLevel = firstLevel + buildSecondLevel(data.child[i]);
    }
    firstLevel = firstLevel + '</ul></li > ';

    return firstLevel;
}

function buildSecondLevel(data) {
    var secondLevel = '';
    if (!data.hasOwnProperty("child")) {
        //secondLevel = '<li><a href=\"'+data.link+'\">'+data.name+'</a></li>';
        secondLevel = '<li>'+buildA(data.name,data.link,null,null)+'</li>';
    } else {
        secondLevel = buildThirdLevel(data);
    }
    return secondLevel;
}
function buildThirdLevel(data) {
    var thirdLevel = '<li> <a href=\"#\"><i class=\"fa fa-files-o fa-fw\"></i> '+data.name+'<span class=\"fa arrow\"></span></a><ul class=\"nav nav-third-level\">';
    for (var i = 0; i < data.child.length; i++) {
        //thirdLevel = thirdLevel + '<li><a href=\"javascript:void(0);\"  onclick=\"openTab(\''+data.child[i].link+ '\')\"; >'+data.child[i].name+'</a></li>';
        thirdLevel = thirdLevel + '<li>'+buildA(data.child[i].name,data.child[i].link,null,null)+'</li>';
        //thirdLevel = thirdLevel + '<li><div onclick=(\"'+data.child[i].link+'\")>'+data.child[i].name+'</div></li>';
    }
    thirdLevel = thirdLevel + '</ul></li>';
    return thirdLevel;
}
//****************************end*****************************


//方法二：以list对象传入，最多3层菜单
//****************************start*****************************
function buildFirstLevel2(data) {
    //var firstLevel = '<li> <a href=\"#\"><i class=\"fa fa-files-o fa-fw\"></i> ' + data.name + '<span class=\"fa arrow\"></span></a> <ul class=\"nav nav-second-level\"> ';
    var firstLevel = '';
    for (var i = 0; i < data.length; i++) {
        if(data[i].PERMISSION_TYPE == 0){
            //一级菜单
            //firstLevel = firstLevel +'<li> <a href=\"#\"><i class=\"fa fa-files-o fa-fw\"></i> '+data[i].PERMISSION_NAME+'<span class=\"fa arrow\"></span></a> <ul class=\"nav nav-second-level\">';
            firstLevel = firstLevel +'<li> '+buildA(data[i].PERMISSION_NAME,data[i].URL,"1","1")+' <ul class=\"nav nav-second-level\">';
            firstLevel = firstLevel + buildSecondLevel2(data,i);
            firstLevel = firstLevel + '</ul></li > ';
        }
    }


    return firstLevel;

}



function buildSecondLevel2(data,parent) {
    var secondLevel = '';
    for (var i = 0; i < data.length; i++) {
        if(data[i].PERMISSION_TYPE == 1 && data[i].PARENT_ID == data[parent].ID){
            //二级菜单
            if(data[i].IS_PARENT == 1){
                secondLevel = secondLevel + buildThirdLevel2(data,i);
            }else{
                //secondLevel = secondLevel + '<li><a href=\"'+data[i].URL+'\">'+data[i].PERMISSION_NAME+'</a></li>';
                secondLevel = secondLevel + '<li>'+buildA(data[i].PERMISSION_NAME,data[i].URL,null,null)+'</li>';
            }
        }

    }
    return secondLevel;
}

function buildThirdLevel2(data,parent){
    //var thirdLevel = '<li> <a href=\"#\"><i class=\"fa fa-files-o fa-fw\"></i> '+data[parent].PERMISSION_NAME+'<span class=\"fa arrow\"></span></a><ul class=\"nav nav-third-level\">';
    var thirdLevel = '<li> '+buildA(data[parent].PERMISSION_NAME,data[parent].URL,null,"1")+'<ul class=\"nav nav-third-level\">';
    for (var i = 0; i < data.length; i++) {
        if(data[i].PERMISSION_TYPE == 2 && data[i].PARENT_ID == data[parent].ID){
            //thirdLevel = thirdLevel + '<li><a href=\"'+data[i].URL+'\">'+data[i].PERMISSION_NAME+'</a></li>';
            thirdLevel = thirdLevel + '<li>'+buildA(data[i].PERMISSION_NAME,data[i].URL,null,null)+'</li>';
            //thirdLevel = thirdLevel + '<li><div onclick=\"openTab(\"data[i].URL\")>'+data[i].PERMISSION_NAME+'<div></li>';
        }
    }
    thirdLevel = thirdLevel + '</ul></li>';
    return thirdLevel;
}
//****************************end*****************************

function openTab(target){
    $("#iframe_container").html("");
    /*var tab = "<iframe src=\""+"main"+"\" class=\"col-lg-12\"></iframe>\"";
    addNode("#iframe_container",tab);*/
    $("#iframe_container").load(target);
}